package com.whynot.frame.designMode.proxy.cglibdynamic;

import java.lang.reflect.Method;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 场景：代理模式的场景基本上是切面。比如想要对方法的前面做一部分处理，计时/日志
 * 
 * cglib的动态代理模式
 * @author chenyao
 * 
 * 优点：可以直接操作类，Spring里面有很多这种机制
 */
public class UserServiceDynamicProxy implements MethodInterceptor {
	private Enhancer enhancer = new Enhancer();
	
	public Object getProxy(Class<?> clazz){
		enhancer.setSuperclass(clazz);
		enhancer.setCallback(this);
		return enhancer.create();
	}

	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("报道");
		proxy.invokeSuper(obj, args);
		System.out.println("去宿舍");
		return null;
	}

	
}
